package com.example.community.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.kms.Endpoint;
import com.example.community.service.AliFile;
import com.example.community.utils.OssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.UUID;
@Service
public class AliFileImpl implements AliFile {
    @Autowired
    private OssProperties ossProperties;
    @Override
    public String upload(InputStream inputStream, String originalFilename) {
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(),ossProperties.getKeyId(),ossProperties.getKeySecret());
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        ossClient.putObject(ossProperties.getBucketName(), fileName, inputStream);
        return fileName;
    }
}
